import { _BasicDataSource } from "../../_BasicDataSource"
import { uuid } from '../../util'
import { BaseEntity } from "../Entitys/BaseEntity";
import { Satellite } from "../Entitys/_Satellite";
import { CreatePosition } from "../../Config";

class SatelliteEntity {
    constructor(Platform, parent, parentOptions) {
        this.id = 'SatelliteEntity_' + Platform.id;
        this.EntityTypeName = 'SatelliteEntity';
        this.parent = parent;
        this.Platform = Platform;

        // 基础平台类型
        this.BaseEntity = new BaseEntity({});
        this.BaseEntity.CesiumPosition = Platform.BaseEntity.CesiumPosition;
        this.Satellite = new Satellite(this.BaseEntity.Entity, Platform, parentOptions);

        this.Satellite.changeSenceType(parent._SenceMode);

        this._changeSceneMode = (type) => {
            this.Satellite.changeSenceType(type);
        }
        this.parent.on('changeSceneMode', this._changeSceneMode);


        // 输出用于图层加载的对象
        this.Entity = this.BaseEntity.Entity;

        this.Platform.addLink(this);
    }
    setPosition(position) {
        this.BaseEntity.CesiumPosition = this.Platform.BaseEntity.CesiumPosition;
        var cylinderPosition = Cesium.Cartesian3.midpoint(this.BaseEntity.CesiumPosition, CreatePosition({
            latitude: position.latitude,
            longitude: position.longitude,
            height: 0
        }), new Cesium.Cartesian3);
        this.Entity.position.setValue(cylinderPosition);
    }
    destroy() {
        this.Platform.removeLink(this);
    }
    set Color(value) {
        this.Satellite.Color = value;
    }
    get Color() {
        return this.Satellite.Color
    }
}

class SatelliteDataSource extends _BasicDataSource {
    constructor(parent, options, parentDataSource) {
        super(uuid(), parent, options);
        this.parentDataSource = parentDataSource;
        this.options = options || {};
    }
    add(id) {
        var platform = this.parentDataSource.GetMappingById(id);
        const _Satellite = new SatelliteEntity(platform, this, this.options);
        this._Mapping[_Satellite.id] = _Satellite;
        this._Entities.add(_Satellite.Entity);
        return _Satellite;
    }
    set Color(value) {
        this.EachMapping((item) => {
            item.Color = value;
        })
    }
    get Color() {
        return this.options.color
    }
}

export {
    SatelliteEntity,
    SatelliteDataSource
}
